<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015-8-25
 * Time: 10:13
 */
namespace common\components\services\message;

use common\models\datas\MessageLog;
use yii\web\NotFoundHttpException;

class JpushMessage extends Message
{
    //AppKey
    const APPKEY = '6a6a8063efc9fa5009da842e';

    //Master Secret
    const MASTER_SECRET = '5005f2d480e831b332d5fea5';

    //推送极光地址
    const PUSH_URL = 'https://api.jpush.cn/v3/push';

    //请求耗时
    const READ_TIMEOUT = 30;

    // 设置User-Agent
    const USER_AGENT = 'JPush-API-PHP-Client';

    // 连接建立最长耗时
    const CONNECT_TIMEOUT = 5;

    //参数
    public $param;

    //请求方式
    public $method = 'POST';


    public function send($id = null,$site_id=null)
    {
        $id=(int)1000;
        if ($id == null) {
            throw new NotFoundHttpException('数据不存在！');
        }
        $message = MessageLog::findOne($id);
        if (empty($message)) {
            throw new NotFoundHttpException('数据不存在！');
        }
        $this->setParam('platform', '["android", "ios"]');
        $this->setParam('audience', 'all');
        $this->setParam('notification', $this->addNotification($message));
        $this->setParam('message', $this->addMessage($message));
        $this->setParam('sms_message', $this->addSmsMessage($message));
        $this->setParam('options', $this->addOptions($id));
        $tojson = json_encode($this->param);

        $res = $this->postUrl($tojson);
        $this->handleResult($res);
        return true;
    }


    public function setParam($key, $value)
    {
        $this->param[$key] = $value;
    }

    public function addNotification($message)
    {
        return ['alert' => $message->content_sys, 'android' => $this->addAndroidNotification($message), 'ios' => $this->addIosNotification($message)];
    }

    public function addAndroidNotification($message)
    {
        return ['title' => $message->content_sys, 'builder_id' => 3, 'extras' => ['news_id'=>$message->id,'content'=>$message->content_sys]];
    }

    public function addIosNotification($message)
    {
        return ['sound' => 'sound.caf', 'badge' => "+1", 'content-available' => '', 'category' => '', 'extras' => ['news_id'=>$message->id,'content'=>$message->content_sys]];
    }

    public function addMessage($message)
    {
        return ['msg_content' => $message->content_sys];

    }

    public function addSmsMessage($message)
    {
        return ['content' => $message->content_sys, 'delay_time' => 0];

    }

    public function addOptions($id)
    {
        return ['sendno' => $id];

    }

    public function postUrl($param)
    {

        if (empty($param)) {
            return false;
        }
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL, self::PUSH_URL);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, false);

        // 设置User-Agent
        curl_setopt($ch, CURLOPT_USERAGENT, self::USER_AGENT);
        // 连接建立最长耗时
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::CONNECT_TIMEOUT);
        // 请求最长耗时
        curl_setopt($ch, CURLOPT_TIMEOUT, self::READ_TIMEOUT);
        // 设置SSL版本
        curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        // 设置Basic认证
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($ch, CURLOPT_USERPWD, self::APPKEY . ":" . self::MASTER_SECRET);

        curl_setopt($ch, CURLOPT_POST, true);//post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
        $data = curl_exec($ch);//运行curl
        // $errorCode = curl_errno($ch);
        curl_close($ch);
        return $data;
    }

    //处理返回结果
    public function handleResult($res)
    {

        $arr = json_decode($res, true);
        print_r($arr);
        die;
        //$res = $arr['eaare'];


    }


}